//
//  NotificationTCell.m
//  students
//
//  Created by 任辰东 on 2024/2/23.
//

#import "NotificationTCell.h"
#import "NSNumber+Timestamp.h"
#import "UILabel+LineSpace.h"

@interface NotificationTCell ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *userLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *stateLabel;
@property (nonatomic, strong) UIImageView *avatarImageView;

@end

@implementation NotificationTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)configCellWithModel:(NotificationModel *)model
{
    self.timeLabel.text = [model.time dateStringWithFormat:@"yyyy-MM-dd HH:mm"];
    self.titleLabel.text =model.title;
    self.userLabel.text = [NSString stringWithFormat:@"%@",model.user_truename];

    if (!IsEmpty(model.touxiang)) {
        self.avatarImageView.hidden = NO;
        self.nameLabel.hidden = YES;
        [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.touxiang]]];
    }else{
        self.avatarImageView.hidden = YES;
        self.nameLabel.hidden = NO;
        if (model.user_truename.length > 2) {
            NSString *name = [model.user_truename substringWithRange:NSMakeRange(model.user_truename.length-2, 2)];
            self.nameLabel.text = name;
        }else{
            self.nameLabel.text = model.user_truename;
        }
    }
    ///0：未读，1：已读）
    self.stateLabel.hidden = model.status.integerValue == 0 ? NO:YES;
    
}

-(void)setupViews
{
    self.contentView.backgroundColor = UIColorFromHex(kColorBG);
    [self.contentView addSubview:self.containerView];
    [self.containerView addSubview:self.timeLabel];
    [self.containerView addSubview:self.titleLabel];
    [self.containerView addSubview:self.userLabel];
    [self.containerView addSubview:self.nameLabel];
    [self.containerView addSubview:self.avatarImageView];
    [self.containerView addSubview:self.stateLabel];
}

-(void)setupLayout
{
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(0);
        make.left.bottom.right.equalTo(self.contentView);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.containerView).offset(15);
        make.left.equalTo(self.containerView).offset(20);
        make.height.width.mas_equalTo(40);
    }];
    self.nameLabel.layer.cornerRadius = 5;
    self.nameLabel.layer.masksToBounds = YES;
    
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.containerView).offset(15);
        make.left.equalTo(self.containerView).offset(20);
        make.height.width.mas_equalTo(40);
    }];
    self.avatarImageView.layer.cornerRadius = 5;
    self.avatarImageView.layer.masksToBounds = YES;
    
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
          
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.left.equalTo(self.containerView).offset(5);
        make.height.width.mas_equalTo(10);
    }];
    self.stateLabel.layer.cornerRadius = 5;
    self.stateLabel.layer.masksToBounds = YES;
    
    [self.userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.containerView).offset(15);
        make.left.equalTo(self.nameLabel.mas_right).offset(10);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.containerView).offset(15);
        make.left.equalTo(self.nameLabel.mas_right).offset(10);
        make.right.equalTo(self.containerView).offset(-15);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.userLabel.mas_bottom).offset(5);
        make.left.equalTo(self.nameLabel.mas_right).offset(10);
        make.right.equalTo(self.containerView).offset(-15);
        make.bottom.equalTo(self.containerView).offset(-15);
    }];
    

}

#pragma mark -- lazyLoad

-(UIView *)containerView
{
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _containerView;
}

-(UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.textColor = UIColorFromHex(kColorPlaceholder);
        _timeLabel.font = UIFontSize(13);
        _timeLabel.textAlignment = NSTextAlignmentRight;
    }
    return _timeLabel;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = UIColorFromHex(kColorSubTitle);
        _titleLabel.font = UIFontSize(15);
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}

-(UILabel *)userLabel
{
    if (!_userLabel) {
        _userLabel = [UILabel new];
        _userLabel.textColor = UIColorFromHex(kColorTitle);
        _userLabel.font = UIBoldFontSize(16);
    }
    return _userLabel;
}

-(UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.backgroundColor = UIColorFromHex(kColorMain);
        _nameLabel.textColor = UIColorFromHex(kColorWhite);
        _nameLabel.font = UIFontSize(13);
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

-(UILabel *)stateLabel
{
    if (!_stateLabel) {
        _stateLabel = [UILabel new];
        _stateLabel.backgroundColor = UIColorFromHex(kColorMain);
    }
    return _stateLabel;
}

-(UIImageView *)avatarImageView
{
    if (!_avatarImageView) {
        _avatarImageView = [UIImageView new];
        _avatarImageView.backgroundColor = [UIColor lightGrayColor];
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageView.clipsToBounds = YES;
    }
    return _avatarImageView;
}

@end
